<template>
  <div style="float: left;margin-top: 5px">
    <el-form ref="searchRef" :model="searchFormData">
      <el-form-item label="关键词" >
        <el-input  type="text" v-model="searchFormData.word"></el-input>
      </el-form-item>
    </el-form>
  </div>
  <div style="height: 40px;margin-top: 5px;float: left;margin-left: 20px;">
    <el-button @click="searchClick">查询</el-button>
    <el-button @click="resetSearchClick">重置</el-button>
    <el-button @click="addClick">新增</el-button>
    <el-button @click="issuedSensitiveWords">下发</el-button>
    <el-button  @click="handleSoft('1')">启动算法</el-button>
    <el-button  @click="handleSoft('0')">停止算法</el-button>
    <el-button  @click="handleSoft('2')">重启算法</el-button>
<!--    <el-button  >实时匹配结果</el-button>-->
    <el-button  >网口1Payload分词</el-button>
    <el-button  >网口2Payload分词</el-button>
  </div>
  <div style="clear: both"></div>
  <div style="height: calc(100% - 60px);">
    <Table  :show-page="true" ref="tableRef" :show-line-number="true"
            :showCheckBox="false" :url="tableUrl" :columns="columns">
      <template  v-slot:handle="slotProps">
        <el-button  @click.stop="editTableData(slotProps.rowData,slotProps.rowIndex)">编辑</el-button>
        <el-button  @click.stop="deleteTableRowData(slotProps.rowData,slotProps.rowIndex)">删除</el-button>
      </template>
    </Table>
  </div>
  <DialogForm ref="editFormRef" width="500" :form="editDialogForm" title="编辑" @validateSuccess="validateSuccess">
  </DialogForm>
</template>

<script setup>

import Table from "@/components/plugs/table/Table.vue";
import DialogForm from "@/components/plugs/form/DialogForm.vue";
import {inject, reactive, ref, toRaw} from "vue";
import {FormItemType} from "@/components/plugs/form/DialogFormInterface.js";
import {AXIOS_NAME} from "@/constant/constant.js";
import {ElMessage, ElMessageBox} from "element-plus";
import {deleteRow, HandleEnum} from "@/utils/utils.ts";

const tableRef = ref(null);
const $axios = inject(AXIOS_NAME);
const searchRef = ref(null);
const editFormRef = ref(null);
const tableUrl = ref("/sensitiveWord/getSensitiveWordPage");

const searchFormData = reactive({
  'word': ''
});

const columns = [
  {field:"word","desc": "敏感词"},
  {field:"hasEnable","desc": "是否开启",width: 200,dicType:"has_enable"},
  {field:"showTime","desc": "创建时间",width: 250},
  {field:"handle","desc": "操作",isSlot:true,width:250}
];

let items = [];
items.push({type: FormItemType.INPUT_HIDDEN,label: 'id',name: 'id'});
items.push({type: FormItemType.INPUT_TEXT,label: '敏感词',name: 'word'});
items.push({type: FormItemType.DIC_SELECT,label: '是否开启',name: 'hasEnable',defaultValue:'1',dicType:"has_enable"});
let editDialogForm = reactive({
  splitCol: 1,
  gutter: 20,
  labelWidth: 115,
  items:items,
  rules:{},
});

const searchClick = () =>{
  tableRef.value.reload(toRaw(searchFormData));
}

const resetSearchClick = () =>{
  for(const  key in searchFormData){
    searchFormData[key] = '';
  }
}

const addClick = () =>{
  editFormRef.value.show();
}

const editTableData = (rowData,rowIndex) =>{
  editFormRef.value.show();
  editFormRef.value.setData(rowData);
}

const deleteTableRowData = (rowData,rowIndex) =>{
  deleteRow('/sensitiveWord/delete',rowData,function (state,data){
    if(state == HandleEnum.SUCCESS){
      tableRef.value.reload();
    }
  });
}

const validateSuccess = (data) =>{
  $axios.post('/sensitiveWord/addOrUpdate',data).then(response => {
    const data = response.data;
    if(data.code == 1){
      tableRef.value.reload();
      editFormRef.value.close();
      ElMessage.info("操作成功！");
      return;
    }
    ElMessage.error(data.msg);
  },error =>{

  });
}

const issuedSensitiveWords = () => {
  ElMessageBox.confirm(
      '是否下发敏感词数据?','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    $axios.post("/sensitiveWord/issuedSensitiveWords").then(response => {

    });
  });
}

const handleSoft = (state) =>{
  ElMessageBox.confirm(
      '是否确定操作敏感词软件?','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    $axios.post("/sensitiveWord/handleSoft",{"state":state}).then(response => {

    });
  });
}
</script>

<style scoped>

</style>